package com.pudu.mydemo.Receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Parcelable;
import android.util.Log;
import android.widget.Toast;



public class NetworkConnectChangedReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {//这个监听wifi的打开与关闭，与wifi的连接无关
            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
            Log.e("wifi", "WIFI_STATE_CHANGED_ACTION wifiState" + wifiState);
            switch (wifiState) {
                case WifiManager.WIFI_STATE_DISABLED:
                    break;
                case WifiManager.WIFI_STATE_DISABLING:
                    break;
                //
            }
        }
        // 这个监听wifi的连接状态即是否连上了一个有效无线路由，当上边广播的状态是WifiManager.WIFI_STATE_DISABLING，和WIFI_STATE_DISABLED的时候，根本不会接到这个广播。
        // 在上边广播接到广播是WifiManager.WIFI_STATE_ENABLED状态的同时也会接到这个广播，当然刚打开wifi肯定还没有连接到有效的无线
        if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
            Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (null != parcelableExtra) {
                NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
                NetworkInfo.State state = networkInfo.getState();
                boolean isConnected = state == NetworkInfo.State.CONNECTED;//当然，这边可以更精确的确定状态
                Log.e("wifi", "NETWORK_STATE_CHANGED_ACTION state" + state);
                Log.e("wifi", "NETWORK_STATE_CHANGED_ACTION isConnected" + isConnected);
              /*  if (isConnected) {
                    if(actionListener != null)
                    actionListener.onSuccess();
                    showWifiCconnected(context);
                } else {
                    if(actionListener != null)
                    actionListener.onFailure();
                    showWifiDisconnected(context);
                }*/
            }
        }
        //这个监听网络连接的设置，包括wifi和移动数据的打开和关闭。.
        //最好用的还是这个监听。wifi如果打开，关闭，以及连接上可用的连接都会接到监听。见log
        // 这个广播的最大弊端是比上边两个广播的反应要慢，如果只是要监听wifi，我觉得还是用上边两个配合比较合适
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            Boolean connected = intent.getBooleanExtra(ConnectivityManager.CONNECTIVITY_ACTION,false);
            if (info != null) {
                Log.e("wifi","CONNECTIVITY_ACTION connected:"+connected);
                Log.e("wifi","CONNECTIVITY_ACTION connected state:"+info.isConnected());
                Log.e("wifi", "CONNECTIVITY_ACTION info.getTypeName()" + info.getTypeName());
                Log.e("wifi", "CONNECTIVITY_ACTION getSubtypeName()" + info.getSubtypeName());
                Log.e("wifi", "CONNECTIVITY_ACTION getState()" + info.getState());
                 Log.e("wifi",
                        "CONNECTIVITY_ACTION getDetailedState()" + info.getDetailedState().name());
                 Log.e("wifi", "CONNECTIVITY_ACTIONgetDetailedState()" + info.getExtraInfo());
                 Log.e("wifi", "CONNECTIVITY_ACTION getType()" + info.getType());
//
                if (info.isConnected()) {
                    if(actionListener != null)
                        actionListener.onSuccess();
                    showWifiCconnected(context);
                } else {
                    if(actionListener != null)
                        actionListener.onFailure();
                    showWifiDisconnected(context);
                }
            }
        }

        if(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION.equals(intent.getAction())) {
            SupplicantState state = (SupplicantState) intent.getParcelableExtra(
                    WifiManager.EXTRA_NEW_STATE);
            NetworkInfo.DetailedState detailedState = WifiInfo.getDetailedStateOf(state);
            Log.e("wifi", "CONNECTIVITY_ACTION DetailedState " + detailedState);
        }

        if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {// 这个监听wifi的连接状态
            Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (null != parcelableExtra) {
                NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
                NetworkInfo.State state = networkInfo.getState();
                if (state == NetworkInfo.State.CONNECTED) {
                    showWifiCconnected(context);
                }
                   else if(state== NetworkInfo.State.DISCONNECTED){
                 showWifiDisconnected(context);
                 }
//昨天写的这个方法，在坐地铁的时候发现，如果地铁上有无效的wifi站点，手机会自动连接，但是连接失败后还是会接到广播，所以不能用了
            }
        }
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {//这个监听网络连接的设置，包括wifi和移动数据 的打开和关闭
            NetworkInfo info = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            if (info != null) {
                if (NetworkInfo.State.CONNECTED == info.getState()) {
                    Intent pushIntent = new Intent();
                   // pushIntent.setClass(context, NotificationService.class);
                } else if (info.getType() == 1) {
                    if (NetworkInfo.State.DISCONNECTING == info.getState())
                        showWifiDisconnected(context);
                }
            }
        }
    }

    /**
     * Interface for callback invocation on an application action
     *
     */
    public interface ActionListener {
        /** The operation succeeded */
        public void onSuccess();

        public void onFailure();
    }

    private ActionListener actionListener;

    public void setActionListener(ActionListener actionListener) {
        this.actionListener = actionListener;
    }

    public void showWifiCconnected(Context context){
        Toast.makeText(context,"the wifi connected", Toast.LENGTH_LONG).show();
    }

    public void showWifiDisconnected(Context context){
        Toast.makeText(context,"the wifi disconnected", Toast.LENGTH_LONG).show();
    }
}
